﻿#include "qtuilist.h"
#include "ui_qtuilist.h"
#include <QColorDialog>
#include <QDebug>

QtUiList::QtUiList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QtUiList)
{
    ui->setupUi(this);
    m_curWidget = ui->widget;
    this->setWindowTitle(QStringLiteral("QSS设计器"));
    ui->stackedWidget->setCurrentIndex(1);
}

QtUiList::~QtUiList()
{
    delete ui;
}

void QtUiList::on_listWidget_itemClicked(QListWidgetItem *item)
{
    if(item->text() == QStringLiteral("QWidget")){
        ui->stackedWidget->setCurrentIndex(0);
        m_curWidget = ui->widget;
    }else if(item->text() == QStringLiteral("QPushButton")){
        ui->stackedWidget->setCurrentIndex(1);
        m_curWidget = ui->button;
    }
    ui->textEdit->setText(m_curWidget->styleSheet());
}

void QtUiList::on_textEdit_textChanged()
{
    m_curWidget->setStyleSheet(ui->textEdit->toPlainText());
    ui->widgetcolor->setStyleSheet("background:"+ui->widget->palette().window().color().name()+";");
}

void QtUiList::on_widgetcolor_clicked()
{
    QColor color = ui->widgetcolor->palette().window().color();
    color = QColorDialog::getColor(color);
    QString qss = "background:"+color.name()+";";
    QString stylesheet = ui->textEdit->toPlainText();
    if(stylesheet.isEmpty()){
        stylesheet = "QWidget{\n\tbackground:"+color.name()+";\n}";
    }else if(stylesheet.contains("background")){
        QRegExp regExp("background.*;");
        regExp.setMinimal(true);
        stylesheet = stylesheet.replace(regExp,qss);
    }else{
        stylesheet.remove("}");
        stylesheet += "background:";
        stylesheet += color.name();
        stylesheet +=  ";\n}\n";
    }
    ui->textEdit->setText(stylesheet);
    ui->widget->setStyleSheet(ui->textEdit->toPlainText());
    ui->widgetcolor->setStyleSheet("background:"+ui->widget->palette().window().color().name()+";");
}

void QtUiList::on_btnNormalBGColor_clicked()
{
    QColor color = ui->widgetcolor->palette().window().color();
    color = QColorDialog::getColor(color);
    QString qss = "";
    if(ui->crystalbtn->isChecked()){
        qss = "background:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,\n"
                "\tstop:0 rgba(%1, %2, %3, 255),\n"
                "\tstop:0.25 rgba(%4, %5, %6, 255),\n"
                "\tstop:0.5 rgba(%7, %8, %9, 255),\n"
                "\tstop:0.65 rgba(%10, %11, %12, 255),\n"
                "\tstop:1 rgba(%13, %14, %15, 255));\n";
        qss = qss.arg(255).arg(255).arg(255)
                .arg(color.red()+(255-color.red())/5*3).arg(color.green()+(255-color.green())/5*3).arg(color.blue()+(255-color.blue())/5*3)
                .arg(color.red()).arg(color.green()).arg(color.blue())
                .arg(color.red()+(255-color.red())/3).arg(color.green()+(255-color.green())/3).arg(color.blue()+(255-color.blue())/3)
                .arg(color.red()+(255-color.red())/3*2).arg(color.green()+(255-color.green())/3*2).arg(color.blue()+(255-color.blue())/3*2);
        //qss += "\nborder:2px solid "+ color.name()+";";
        //qss += "\nborder-radius:8px;";
    }else{
        qss = "background:"+color.name()+";\n";
    }
    QString stylesheet = ui->textEdit->toPlainText();

    QRegExp regExp("(QPushButton[ ]*\\{.*)background:.*;(.*)");
    regExp.setMinimal(true);

    if(stylesheet.indexOf(regExp)==-1){
        stylesheet += "QPushButton{\n\t";
        stylesheet += qss;
        stylesheet += "\n}\n";
    }else{
        stylesheet.replace(regExp,"\\1"+qss+"\\2");
    }
    ui->textEdit->setText(stylesheet);
    ui->button->setStyleSheet(ui->textEdit->toPlainText());
    ui->btnNormalBGColor->setStyleSheet("background:"+color.name()+";");
}

void QtUiList::on_btnHoverBGColor_clicked()
{
    QColor color = ui->widgetcolor->palette().window().color();
    color = QColorDialog::getColor(color);
    QString qss = "";
    if(ui->crystalbtn->isChecked()){
        qss = "background:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,\n"
                "\tstop:0 rgba(%1, %2, %3, 255),\n"
                "\tstop:0.25 rgba(%4, %5, %6, 255),\n"
                "\tstop:0.5 rgba(%7, %8, %9, 255),\n"
                "\tstop:0.65 rgba(%10, %11, %12, 255),\n"
                "\tstop:1 rgba(%13, %14, %15, 255));\n";
        qss = qss.arg(255).arg(255).arg(255)
                .arg(color.red()+(255-color.red())/5*3).arg(color.green()+(255-color.green())/5*3).arg(color.blue()+(255-color.blue())/5*3)
                .arg(color.red()).arg(color.green()).arg(color.blue())
                .arg(color.red()+(255-color.red())/3).arg(color.green()+(255-color.green())/3).arg(color.blue()+(255-color.blue())/3)
                .arg(color.red()+(255-color.red())/3*2).arg(color.green()+(255-color.green())/3*2).arg(color.blue()+(255-color.blue())/3*2);
        //qss += "\nborder:2px solid "+ color.name()+";";
        //qss += "\nborder-radius:8px;";
    }else{
        qss = "background:"+color.name()+";\n";
    }
    QString stylesheet = ui->textEdit->toPlainText();

    QRegExp regExp("(QPushButton[ ]*:[ ]*hover[ ]*\\{.*)background:.*;(.*)");
    regExp.setMinimal(true);

    if(stylesheet.indexOf(regExp)==-1){
        stylesheet += "QPushButton:hover{\n\t";
        stylesheet += qss;
        stylesheet += "\n}\n";
    }else{
        stylesheet.replace(regExp,"\\1"+qss+"\\2");
    }
    ui->textEdit->setText(stylesheet);
    ui->button->setStyleSheet(ui->textEdit->toPlainText());
    ui->btnHoverBGColor->setStyleSheet("background:"+color.name()+";");
}

void QtUiList::on_btnPressedBGColor_clicked()
{
    QColor color = ui->widgetcolor->palette().window().color();
    color = QColorDialog::getColor(color);
    QString qss = "";
    if(ui->crystalbtn->isChecked()){
        qss = "background:qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,\n"
                "\tstop:0 rgba(%1, %2, %3, 255),\n"
                "\tstop:0.25 rgba(%4, %5, %6, 255),\n"
                "\tstop:0.5 rgba(%7, %8, %9, 255),\n"
                "\tstop:0.65 rgba(%10, %11, %12, 255),\n"
                "\tstop:1 rgba(%13, %14, %15, 255));\n";
        qss = qss.arg(255).arg(255).arg(255)
                .arg(color.red()+(255-color.red())/5*3).arg(color.green()+(255-color.green())/5*3).arg(color.blue()+(255-color.blue())/5*3)
                .arg(color.red()).arg(color.green()).arg(color.blue())
                .arg(color.red()+(255-color.red())/3).arg(color.green()+(255-color.green())/3).arg(color.blue()+(255-color.blue())/3)
                .arg(color.red()+(255-color.red())/3*2).arg(color.green()+(255-color.green())/3*2).arg(color.blue()+(255-color.blue())/3*2);
        //qss += "\nborder:2px solid "+ color.name()+";";
        //qss += "\nborder-radius:8px;";
    }else{
        qss = "background:"+color.name()+";\n";
    }
    QString stylesheet = ui->textEdit->toPlainText();

    QRegExp regExp("(QPushButton[ ]*:[ ]*pressed[ ]*\\{.*)background:.*;(.*)");
    regExp.setMinimal(true);

    if(stylesheet.indexOf(regExp)==-1){
        stylesheet += "QPushButton:pressed{\n\t";
        stylesheet += qss;
        stylesheet += "\n}";
    }else{
        stylesheet.replace(regExp,"\\1"+qss+"\\2");
    }
    ui->textEdit->setText(stylesheet);
    ui->button->setStyleSheet(ui->textEdit->toPlainText());
    ui->btnPressedBGColor->setStyleSheet("background:"+color.name()+";");
}

void QtUiList::on_crystalbtn_clicked(bool checked)
{

}
